﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BestCode.Creds.Media
{
    public class FeaturesBusiness : ICredsMediaFeatures
    {
        public CREDS_MEDIAEntities Context = EFDataFactory.Current.EFContext;

        public void Save(int fieldDefinitionId, int entityId, int? entityChildId, byte[] resource, bool isAnverseFace, string description, string fileName, string entityInfo)
        {
            try
            {
                var data = Context.Resources.Single(p => p.FieldDefinitionId == fieldDefinitionId && p.IsAnverseFace == isAnverseFace);
                Context.Resources.Remove(data);
                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                
            }

            Resource entity = new Resource();
            entity.FieldDefinitionId = fieldDefinitionId;
            entity.ChildEntityId = entityChildId;
            entity.EntityId = entityId;
            entity.GeneratedDate = DateTime.Now;
            entity.Generated = resource;
            entity.Description = description;
            entity.IsAnverseFace = isAnverseFace;
            entity.FileName = fileName;
            entity.EntityInfo = entityInfo;
            Context.Resources.Add(entity);
            Context.SaveChanges();
        }

        public List<MediaFeature> GetByEntity(int entityId)
        {
            var datas = Context.Resources.Where(p => p.EntityId == entityId);
            var result = datas.Select(p => new MediaFeature()
            {
                FieldDefinitionId = p.FieldDefinitionId,
                EntityId = p.EntityId,
                EntityChildId = p.ChildEntityId,
                Resource = p.Generated,
                CreationDate = p.GeneratedDate,
                IsAnverseFace = p.IsAnverseFace,
                Description = p.Description,
                FileName = p.FileName,
                EntityInfo = p.EntityInfo
            }).ToList();

            return result;
        }

        public List<MediaFeature> GetByentityChild(int entityChildId)
        {
            var datas = Context.Resources.Where(p => p.ChildEntityId == entityChildId);
            var result = datas.Select(p => new MediaFeature()
            {
                FieldDefinitionId = p.FieldDefinitionId,
                EntityId = p.EntityId,
                EntityChildId = p.ChildEntityId,
                Resource = p.Generated,
                CreationDate = p.GeneratedDate,
                IsAnverseFace = p.IsAnverseFace,
                Description = p.Description,
                FileName = p.FileName,
                EntityInfo = p.EntityInfo
            }).ToList();

            return result;
        }
    }


    public class EFDataFactory
    {
        static EFDataFactory instance = null;
        public static EFDataFactory Current
        {
            get
            {
                if (instance == null)
                    instance = new EFDataFactory();
                return instance;
            }
        }


        public CREDS_MEDIAEntities EFContext { get; set; }

        public EFDataFactory()
        {
            EFContext = new CREDS_MEDIAEntities();
        }


    }

}
